/* import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

 */
import { setToken, getToken, removeToken } from '@/utils/auth'
import { login, getInfo, getUserDetailInfo } from '@/api/user'
import { Message } from 'element-ui'
export default {
  namespaced: true,
  state: {
    // token从本地获取，否则刷新会清空token
    token: getToken() || '',
    // 用户信息
    userInfo: {}
  },
  mutations: {
    // token 存储
    updateToken(state, token) {
      state.token = token
      // 将token存储在cookies中
      setToken(token)
    },
    // 删除token
    removeToken(state) {
      // 删除cookie中的token
      removeToken()
      // 删除state中的token
      state.token = ''
    },
    // 获取用户信息
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 删除用户信息
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    // 登录
    async Login(ctx, loginForm) {
      const res = await login(loginForm)
      ctx.commit('updateToken', res.data)
      Message.success(res.message)
    },
    // 获取用户信息和用户详情信息
    async getUserInfo(ctx) {
      const res1 = await getInfo()
      const res2 = await getUserDetailInfo(res1.data.userId)
      // console.log(res1.data.roles.menus)
      ctx.commit('updateUserInfo', { ...res1.data, ...res2.data })
      return res1.data.roles.menus
    },
    // 退出登录
    logout(ctx) {
      ctx.commit('removeToken')
      ctx.commit('removeUserInfo')
    }
  }
}
